package com.lagou.edu.controller;

import com.lagou.edu.dao.Resume;
import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Create By Waiting on 2020/2/25
 */
@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    ResumeService resumeService;

    @RequestMapping("/login")
    public void login(String username, String password, HttpServletResponse response) throws IOException {
        if ("admin".equals(username) && "admin".equals(password)) {
            Cookie cookie = new Cookie("user", username + ":" + password);
            cookie.setPath("/");
            cookie.setMaxAge(3600);
            response.addCookie(cookie);
            response.sendRedirect("/resume.html");
        } else {
            response.getWriter().write("Login Fail");
        }

    }

    @RequestMapping("/findAll")
    @ResponseBody
    public List<Resume> findAll() {
        List<Resume> all = resumeService.findAll();
        return all;
    }

    @PostMapping("/save")
    public String save(Resume resume) {
        resumeService.save(resume);
        return "/resume.html";
    }

    @GetMapping("/del")
    public void del(Integer id) {
        resumeService.delete(id);
    }
}
